package tw.com.softleader.steven;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

public class GetLinePicView {
	
	private static final int DEFAULT_RETRY_WEIGHT = 5;

	private JFrame frmGetlinepic;
	private JTextField authKey;
	private JTextField accessToken;
	private JTextField retry;
	private JButton test;
	private JScrollPane scrollPane;
	private JList<String> list;
	
	private Execute execute = new Execute();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GetLinePicView window = new GetLinePicView();
					window.frmGetlinepic.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public GetLinePicView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmGetlinepic = new JFrame();
		frmGetlinepic.setTitle("GetLinePic");
		frmGetlinepic.setSize(250, 340);
		
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
	    int x = (int) ((dimension.getWidth() / 2) - (frmGetlinepic.getWidth() / 2));
	    int y = (int) ((dimension.getHeight() / 2) - (frmGetlinepic.getHeight() / 2));
	    frmGetlinepic.setLocation(x, y);
	    
		frmGetlinepic.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{238, 0};
		gridBagLayout.rowHeights = new int[] {10, 10, 10, 10, 10, 76, 10, 10, 10, 10, 0};
		gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		frmGetlinepic.getContentPane().setLayout(gridBagLayout);
		
		JLabel lblAuthkey = new JLabel("AuthKey (Android用，ios 請勿輸入)");
		GridBagConstraints gbc_lblAuthkey = new GridBagConstraints();
		gbc_lblAuthkey.fill = GridBagConstraints.BOTH;
		gbc_lblAuthkey.insets = new Insets(0, 0, 5, 0);
		gbc_lblAuthkey.gridx = 0;
		gbc_lblAuthkey.gridy = 0;
		frmGetlinepic.getContentPane().add(lblAuthkey, gbc_lblAuthkey);
		
		authKey = new JTextField();
		lblAuthkey.setLabelFor(authKey);
		GridBagConstraints gbc_authKey = new GridBagConstraints();
		gbc_authKey.fill = GridBagConstraints.BOTH;
		gbc_authKey.insets = new Insets(0, 0, 5, 0);
		gbc_authKey.gridx = 0;
		gbc_authKey.gridy = 1;
		frmGetlinepic.getContentPane().add(authKey, gbc_authKey);
		authKey.setColumns(10);
		
		JLabel lblAccesstoken = new JLabel("accessToken (ios用，android 請勿輸入)");
		GridBagConstraints gbc_lblAccesstoken = new GridBagConstraints();
		gbc_lblAccesstoken.fill = GridBagConstraints.BOTH;
		gbc_lblAccesstoken.insets = new Insets(0, 0, 5, 0);
		gbc_lblAccesstoken.gridx = 0;
		gbc_lblAccesstoken.gridy = 2;
		frmGetlinepic.getContentPane().add(lblAccesstoken, gbc_lblAccesstoken);
		
		accessToken = new JTextField();
		lblAccesstoken.setLabelFor(accessToken);
		GridBagConstraints gbc_accessToken = new GridBagConstraints();
		gbc_accessToken.fill = GridBagConstraints.BOTH;
		gbc_accessToken.insets = new Insets(0, 0, 5, 0);
		gbc_accessToken.gridx = 0;
		gbc_accessToken.gridy = 3;
		frmGetlinepic.getContentPane().add(accessToken, gbc_accessToken);
		accessToken.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("選擇圖片編號");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.fill = GridBagConstraints.BOTH;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 4;
		frmGetlinepic.getContentPane().add(lblNewLabel, gbc_lblNewLabel);
		
		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 5;
		frmGetlinepic.getContentPane().add(scrollPane, gbc_scrollPane);
		
		list = new JList<>(execute.getImageNo());
		lblNewLabel.setLabelFor(list);
		scrollPane.setViewportView(list);
		
		JLabel label = new JLabel("重試次數(小於-1為無限次)");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.fill = GridBagConstraints.BOTH;
		gbc_label.insets = new Insets(0, 0, 5, 0);
		gbc_label.gridx = 0;
		gbc_label.gridy = 6;
		frmGetlinepic.getContentPane().add(label, gbc_label);
		
		retry = new JTextField();
		label.setLabelFor(retry);
		retry.setText(Integer.toString(DEFAULT_RETRY_WEIGHT));
		GridBagConstraints gbc_retry = new GridBagConstraints();
		gbc_retry.fill = GridBagConstraints.BOTH;
		gbc_retry.insets = new Insets(0, 0, 5, 0);
		gbc_retry.gridx = 0;
		gbc_retry.gridy = 7;
		frmGetlinepic.getContentPane().add(retry, gbc_retry);
		retry.setColumns(10);
		
		test = new JButton("測試Key/Token");
		test.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean valid = check(authKey.getText(), accessToken.getText());
				String message = "";
				if (valid) {
					message = execute.doCheckToken(authKey.getText(), accessToken.getText());
				} else {
					message = "輸入內容錯誤，請重新執行。";
				}
				JOptionPane.showMessageDialog(null, message, "GetLinePic: 測試Key/Token。", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		GridBagConstraints gbc_test = new GridBagConstraints();
		gbc_test.fill = GridBagConstraints.BOTH;
		gbc_test.insets = new Insets(0, 0, 5, 0);
		gbc_test.gridx = 0;
		gbc_test.gridy = 8;
		frmGetlinepic.getContentPane().add(test, gbc_test);
		
		JButton start = new JButton("開始抓圖");
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean valid = check(authKey.getText(), accessToken.getText());
				String message = "";
				if (valid) {
					message = execute.doSubmit(authKey.getText(), accessToken.getText(), list.getSelectedValuesList(), Integer.parseInt(retry.getText()));
				} else {
					message = "輸入內容錯誤，請重新執行。";
				}
				JOptionPane.showMessageDialog(null, message, "GetLinePic: 請檢查我的貼圖。", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		GridBagConstraints gbc_start = new GridBagConstraints();
		gbc_start.fill = GridBagConstraints.BOTH;
		gbc_start.gridx = 0;
		gbc_start.gridy = 9;
		frmGetlinepic.getContentPane().add(start, gbc_start);
		
		JOptionPane.showMessageDialog(null, "請先刪除綁定的電話號碼", "GetLinePic: 提示", JOptionPane.INFORMATION_MESSAGE);
		JOptionPane.showMessageDialog(null, "取得裝置中，請勿複製任何東西...並等候。", "GetLinePic: 提示", JOptionPane.INFORMATION_MESSAGE);
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String deviceAccessToken = execute.getAccessTokenFromDevice();
					if (StringUtils.isNotEmpty(deviceAccessToken) && ('u' == deviceAccessToken.charAt(0)) && ('=' == deviceAccessToken.charAt(deviceAccessToken.length() - 1))) {
						JOptionPane.showMessageDialog(null, "成功取得裝置", "GetLinePic: 提示", JOptionPane.INFORMATION_MESSAGE);
						accessToken.setText(deviceAccessToken);
					} else {
						JOptionPane.showMessageDialog(null, "請裝裝置透過USB連接電腦並重新執行此程式。或自行輸入。", "GetLinePic: 提示", JOptionPane.INFORMATION_MESSAGE);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private static boolean check(String authKey, String accessToken) {
		boolean result = Boolean.TRUE;
		if ((authKey == null || "".equals(authKey.trim())) &&
			(accessToken == null || "".equals(accessToken.trim()))
		) {
			result = Boolean.FALSE;
		}
		return result;
	}
	
}
